package ru.compscicenter.databases.zaikina.utils;

import ru.compscicenter.databases.zaikina.common.commands.EmptyCommand;
import ru.compscicenter.databases.zaikina.common.commands.StringListCommand;
import ru.compscicenter.databases.zaikina.common.commands.SysCommand;
import ru.compscicenter.databases.zaikina.common.commands.SysCommandType;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class TestNodeConnection {

    private static ObjectInputStream in;// = new ObjectInputStream(socket.getInputStream());
    private static ObjectOutputStream out;// = new ObjectOutputStream(socket.getOutputStream());

    public static StringListCommand formStringListCommand(String word) {
        ArrayList<String> strings = new ArrayList<String>();
        strings.add(word);
        return new StringListCommand(SysCommandType.GetSameWords, strings, -1);
    }

    private static String readAnswer(Socket socket) throws IOException {
        String text;
        try {
            text = (String) in.readObject();
        } catch (ClassNotFoundException ex) {
            return ex.getMessage();
        }
        return text;
    }

    /*private static void write() throws IOException {
        BufferedWriter writer = new BufferedWriter(out);
        writer.write(formStringListCommand("Some").toJSONString());
        writer.flush();
    }*/

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 10003);
        in = new ObjectInputStream(socket.getInputStream());
        out = new ObjectOutputStream(socket.getOutputStream());

        //System.out.println("Answer: " + readAnswer(socket));

        out.writeObject(formStringListCommand("Some").toJSONString());
        //BufferedWriter writer = new BufferedWriter(out);
        //writer.write(formStringListCommand("Some").toJSONString());
        //writer.flush();
        //writer.close();

        System.out.println("Answer: " + readAnswer(socket));
    }
}
